import requests
import re
import os

if not os.path.exists('三寸人间'):
    os.makedirs('三寸人间')

root_url = 'https://www.xbiquge.la'

response = requests.get(url=root_url + '/10/10489/')
response.encoding = response.apparent_encoding
html_data = response.text
# print(html_data)

# 解析数据
"""
<dd><a href='/10/10489/4534454.html' >写在连载前</a></dd>
<dd><a href='/10/10489/4534454.html' >写在连载前</a></dd>
"""
result_list = re.findall("<dd><a href='(.*?)' >.*?</a></dd>", html_data, re.S)
# print(result_list)
result_list_ = result_list[1:11]
print(result_list_)


# for result in result_list_:


# 递归函数 ：在函数内部调用当前函数
def download(url, times=5):
    """
    下载小说
    times: 控制异常重试的次数
    """
    try:
        all_url = root_url + url

        response_2 = requests.get(all_url)
        response_2.encoding = 'utf-8'
        html_data_2 = response_2.text

        # 提取标题
        title = re.findall('<h1>(.*?)</h1>', html_data_2, re.S)[0]
        content = re.findall('<div id="content">(.*?)<p>.*?</p></div>', html_data_2, re.S)
        # print(title, content)

        # 数据保存
        with open('三寸人间/' + title + '.txt', mode='w', encoding='utf-8') as f:
            f.write(content[0].replace('&nbsp;', '').replace('<br />', ''))
            print(f'正在下载：{title}')
    except Exception as e:
        print(e)
        if times >= 1:
            download(all_url, times=times - 1)


for url in result_list:
    download(url)
